import { ref } from 'vue';

import { deserialize } from '@/plugins/reflect-metadata';
import type { ListState } from '@/types';

import { getProductFaceValue } from '../api';
import {
  TransFaceValueItem,
  TransFaceValueList,
  TransFaceValueQuery,
} from '../models/TransSelectQuery';

const useFaceValueList = () => {
  const params = ref<TransFaceValueQuery>(new TransFaceValueQuery());
  const state = ref<ListState<TransFaceValueItem>>({
    data: [],
    loading: false,
    total: 0,
  });

  const fetchData = async () => {
    try {
      state.value.loading = true;
      const { data } = await getProductFaceValue(params.value);
      const resultData = deserialize(data, TransFaceValueList);
      state.value.data = resultData.items;
      state.value.total = resultData.total;
    } catch (error) {
      //
    } finally {
      state.value.loading = false;
    }
  };

  return {
    params,
    state,
    fetchData,
  };
};

export default useFaceValueList;
